<?php
/**
 * @user:andy
 * @date:2021/6/20
 * @company:http://www.sjdphp.com
 * @desc: 处理操作日志
 */

namespace app\common\middleware;


use app\common\model\BackLog;
use app\common\server\SystemLog;
use app\common\model\OperateLog as OperateLogModel;
use app\common\model\Menu as MenuModel;
class CheckOperateLog
{
    public function handle($request,\Closure $next){
        ## 获取操作的数据
        $param = $request->param();

        ## 获取模型名称
        $module = $request->root();
        ## 获取控制器名称
        $controller = $request->controller();
        ## 获取方法名称
        $action = $request->action();

        ## 拼接请求地址
        $path = $module . '/' . $controller . '/' . $action;

        ## 过滤的方法
        $filterAttr = array('/back/Index/index','/back/Index/welcome','/back/Menu/initMenu','/back/Index/images','/back/Index/favicon.ico');

        ## 获取当前登陆的管理员日志
        $backLog = BackLog::where(array("back_id" => $request->BackInfo["back_id"],'log_status' => 0))->find();

        if(!in_array($path,$filterAttr)){
            ## 获取菜单标识
            try{
                $menuInfo = MenuModel::where(array("menu_controller" => lcfirst($controller)))->field("menu_name")->find();
            }catch(\Exception $e){
                SystemLog::insertLog($e->getMessage());
            }

            ## 存储操作类型
            $operateName = '';
            ## 判断方法名称
            switch ($action){
                case "lists":
                    $operateName = "查看{$menuInfo['menu_name']}列表";
                    break;
                case 'add':
                    ## 增加cookie过期时间
                    $backLog->end_time = $backLog["end_time"] + config("project.back_cookie.add_handle");
                    $operateName = "添加" . $menuInfo['menu_name'];
                    break;
                case 'edit':
                    ## 增加cookie过期时间
                    $backLog->end_time = $backLog["end_time"] + config("project.back_cookie.edit_handle");
                    $operateName = '编辑' . $menuInfo['menu_name'];
                    break;
                case 'del':
                    $backLog->end_time = $backLog["end_time"] + config("project.back_cookie.del_handle");
                    ## 增加cookie过期时间
                    $operateName = '删除' . $menuInfo['menu_name'];
                    break;
                case "status":
                    $operateName = '状态' . $menuInfo["menu_name"];
                    break;
                case "uploads":
                    $operateName = '上传图片';
                    break;
                default:
                    $operateName = '未知的操作类型';
                    break;
            }


            ## 拼接操作日志
            $operatorParam = array(
                'back_id'      => $request->BackInfo['back_id'],
                'back_name'    => $request->BackInfo["back_name"],
                'operate_name' => $operateName,
                'request_path' => $path,
                'param'        => json_encode($param),
                'log_status'   => 1
            );

            try{
                ## 修改登陆日志
                $backLog->save();
                ## 操作日志入库
                OperateLogModel::create($operatorParam);
            }catch(\Exception $e){
                SystemLog::insertLog($e->getMessage());
            }
        }
        return $next($request);
    }
}